<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <!-- 左边 -->
    <el-col>
      <!-- 名称应该变成 对应的节点中的name -->
      <span>{{ treeData.name }}</span>
    </el-col>

    <!-- 右边 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span>
              操作
              <i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepart">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentsApi } from '@/api/departments'
// 该组件需要对外开放属性 外部需要提供一个对象 对象里需要有name  manager
export default {
  name: 'TreeTools',
  // props可以用数组来接收数据 也可以用对象来接收
  // props: {   props属性: {  配置选项 }  }
  props: {
    //   定义一个props属性
    treeData: {
      type: Object, // 对象类型
      default: () => ({})
    },

    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async delDepart() {
      console.log('删除' + this.treeData.id)
      try {
        //  Elemetn 弹出框
        await this.$confirm('确认删除')

        // 删除请求
        const res = await delDepartmentsApi(this.treeData.id)
        console.log(res)

        // Element 弹出框
        this.$message.success('删除成功')

        // 字传父  删除父级数据
        this.$emit('updateList')
      } catch (e) {
        // Element 弹出框
        this.$message.success('删除失败')
        console.log('删除失败', e)
      }
    }
  }
}
</script>
